import { Component } from 'react'
import { View, Text, Button } from '@tarojs/components'
import Taro from '@tarojs/taro'
import './index.less'
import AddQuestion from '../components/addquestion/addquestion'

function getStore(key) {
  let str = Taro.getStorageSync(key)
  if (!str) {
    return []
  }
  return JSON.parse(obj)
}

function setStore(key, obj) {
  let str = obj;
  if (typeof obj === 'object') {
    str = JSON.stringify(obj)
  }
  Taro.setStorageSync(key, str)
}

export default class Index extends Component {

  questionClick() {
    alert('questionClick')
  }

  state = {
    showQuestionModal: false,
    questionList: getStore('questions')
  }

  closeQuestion() {
    this.setState({
      showQuestionModal: false
    })
  }

  receiveQuestion(options) {
    let { questionList } = this.state
    questionList.push(options)
    this.setState({
      questionList
    }, () => setStore('questions', questionList))
    this.closeQuestion
  }

  render() {
    return (
      <View className='container'>
        <Text className='title'>问答Demo</Text>
        <AddQuestion />
        <Button className='btn-question' onClick={this.questionClick.bind(this)}>提问</Button>
      </View>
    )
  }

}
